package com.example.italkapp2.controller.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.example.italkapp2.R;
import com.example.italkapp2.controller.adapter.GroupListAdapter;
import com.example.italkapp2.model.Model;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.easeui.constants.EaseConstant;
import com.hyphenate.exceptions.HyphenateException;

import java.util.ArrayList;
import java.util.List;

//群组列表页面
public class GroupListActivity extends Activity {
    private ListView lv_group_list;

    private GroupListAdapter groupListAdapter;
    private LinearLayout ll_group_list;
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_list);
        
        initView();
        initData();
        initListener();
    }

    private void initView() {
        lv_group_list=findViewById(R.id.lv_group_list);

        //添加头布局
        View headerView = View.inflate(this, R.layout.header_group_grouplist, null);
        lv_group_list.addHeaderView(headerView);

        ll_group_list = headerView.findViewById(R.id.ll_group_list);
        toolbar = findViewById(R.id.toolbar);
    }

    private void initData(){
        groupListAdapter = new GroupListAdapter(this);
        lv_group_list.setAdapter(groupListAdapter);

        //从环信服务器获取所有群的信息
        getGroupsFromServer();
    }

    private void initListener() {
        //listView 条目点击事件
        lv_group_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                //Log.e("TAG","positon:"+position);
                //position为0是第一个固定的创群的item
                if (position==0){
                    return;
                }

                Intent intent = new Intent(GroupListActivity.this, ChatActivity.class);
                //传递群名称
                EMGroup emGroup=EMClient.getInstance().groupManager().getAllGroups().get(position-1);
                intent.putExtra("titleName",emGroup.getGroupId());
                //传递会话类型
                intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE,EaseConstant.CHATTYPE_GROUP);
                //传递群ID
                intent.putExtra(EaseConstant.EXTRA_CONVERSATION_ID,emGroup.getGroupId());
                startActivity(intent);
            }
        });

        //跳转到新建群
        ll_group_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GroupListActivity.this, NewGroupActivity.class);
                startActivity(intent);
            }
        });

        //返回键监听
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GroupListActivity.this.finish();
            }
        });
    }



    private void getGroupsFromServer() {
        Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                //从网络获取数据
                try {
                    final List<EMGroup> emGroups=EMClient.getInstance().groupManager().getJoinedGroupsFromServer();
                    //更新页面
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            groupListAdapter.refresh(emGroups);
                            refresh();
                            Toast.makeText(GroupListActivity.this,"加载群信息成功",Toast.LENGTH_SHORT).show();

                        }
                    });
                } catch (HyphenateException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(GroupListActivity.this,"加载群信息失败"+e.toString(),Toast.LENGTH_SHORT).show();
                        }
                    });
                }

            }
        });
    }

    //刷新当前群组列表
    private void refresh(){
        groupListAdapter.refresh(EMClient.getInstance().groupManager().getAllGroups());
    }



    @Override
    protected void onResume() {
        super.onResume();
        //如果GroupListActivity有新的群生成会立即刷新activity页面
        refresh();
    }
}